package guestBook;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.PreparedStatement;

public class Input extends JInternalFrame {
	GuestBookMain gbm;
	private JTextField idIn;
	private JLabel label;
	private JLabel label_1;
	private JTextField subIn;
	private JScrollPane scrollPane;
	private JButton btnInput;
	private JTextArea contentMsg;

	public Input(GuestBookMain g) {		
		super("방명록입력",true,true,true,true);
		this.gbm=g;
		
		setBounds(0, 0, 429, 449);
		getContentPane().setLayout(null);
		getContentPane().add(getIdIn());
		getContentPane().add(getLabel());
		getContentPane().add(getLabel_1());
		getContentPane().add(getSubIn());
		getContentPane().add(getScrollPane());
		getContentPane().add(getBtnInput());
	}
	private JTextField getIdIn() {
		if (idIn == null) {
			idIn = new JTextField();
			idIn.setColumns(10);
			idIn.setBounds(76, 11, 209, 21);
		}
		return idIn;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("ID");
			label.setBounds(12, 14, 31, 15);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("Subject");
			label_1.setBounds(12, 45, 52, 15);
		}
		return label_1;
	}
	private JTextField getSubIn() {
		if (subIn == null) {
			subIn = new JTextField();
			subIn.setColumns(10);
			subIn.setBounds(76, 42, 322, 21);
		}
		return subIn;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 70, 386, 340);
			scrollPane.setViewportView(getContentMsg());
		}
		return scrollPane;
	}
	private JButton getBtnInput() {
		if (btnInput == null) {
			btnInput = new JButton("Input");
			btnInput.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String sql = "insert into guestbook (serial, mid, subject, content) values (gb_serial.nextval,?,?,?)";
					
					try{
						PreparedStatement pstmt = gbm.conn.prepareStatement(sql);
						pstmt.setString(1, idIn.getText());
						pstmt.setString(2, subIn.getText());
						pstmt.setString(3, contentMsg.getText());
						
						int a = pstmt.executeUpdate();
						
						if(gbm.select!=null){
							gbm.select.setData();
						}
						if(a>0){
							idIn.setText("");
							subIn.setText("");
							contentMsg.setText("");
							
							JOptionPane.showMessageDialog(null, "방명록이 저장되었습니다.");
						}
						
						
					}catch(Exception ex){
						
					}
					
				}
			});
			btnInput.setBounds(297, 10, 101, 23);
		}
		return btnInput;
	}
	private JTextArea getContentMsg() {
		if (contentMsg == null) {
			contentMsg = new JTextArea();
			contentMsg.setText("내용을 입력하세요...");
		}
		return contentMsg;
	}
}
